#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <unistd.h>
#include <stdio.h>


void minimize_window(Display *display, Window window) {
    Atom net_wm_state = XInternAtom(display, "_NET_WM_STATE", False);
    Atom net_wm_state_hidden = XInternAtom(display, "_NET_WM_STATE_HIDDEN", False);

    XEvent e;
    e.xclient.type = ClientMessage;
    e.xclient.message_type = net_wm_state;
    e.xclient.display = display;
    e.xclient.window = window;
    e.xclient.format = 32;
    e.xclient.data.l[0] = 1;  // _NET_WM_STATE_ADD
    e.xclient.data.l[1] = net_wm_state_hidden;
    e.xclient.data.l[2] = 0;  // 第二个属性（未使用）
    e.xclient.data.l[3] = 0;  // 不指定源

    XSendEvent(display, DefaultRootWindow(display), False,
               SubstructureRedirectMask | SubstructureNotifyMask, &e);
    XFlush(display);
}

int main() {
    Display *display = XOpenDisplay(NULL);
    if (!display) return 1;

    Window window = XCreateSimpleWindow(display, DefaultRootWindow(display),
                              0, 0, 400, 300, 1, 0, 0);
    XStoreName(display, window, "最小化测试窗口");
    XMapWindow(display, window);
    XFlush(display);

    sleep(2);  // 等待窗口显示
    printf("wait minimal");

    // 方法1：EWMH 标准最小化
    minimize_window(display, window);

    // 方法2：传统最小化（可选）
    // XIconifyWindow(display, window, DefaultScreen(display));

    sleep(10);  // 观察效果
    XCloseDisplay(display);
    return 0;
}
